<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <user-search @search="reload" />
      <!-- 数据表格 -->
      <!--      expand-change 展开行事件，row-click：某一行点击时事件-->
      <ele-pro-table
        @expand-change="expandChange"
        :row-key='getRowKeys'
        :expand-row-keys="expands"
        :row-style="{height: '45px'}"
        class="ele-table-class"
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        cache-key="systemUserTable"
        width=100
      >
        <!--展开行-->
        <template v-slot:expand="{ row }">
          <el-table class="table-expand table-class" :data="tableData" style="z-index: 99;overflow-x: auto">
            <el-table-column  label="套餐名称" align="center">
              <template slot-scope="scope">
                <div class="font-overflow">{{scope.row.mealName}}</div>
              </template>
            </el-table-column>
            <el-table-column label="套餐零售价" align="center">
              <template slot-scope="scope">
                <div class="font-overflow">{{scope.row.retailPrice}}</div>
              </template>
            </el-table-column>
            <el-table-column label="代理拿货价" align="center">
              <template slot-scope="scope">
                <div class="font-overflow">{{scope.row.deliveryPrice}}</div>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            新建
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
          >
            删除
          </el-button>
          <!--          <el-button-->
          <!--            size="small"-->
          <!--            icon="el-icon-upload2"-->
          <!--            class="ele-btn-icon"-->
          <!--            @click="openImport"-->
          <!--          >-->
          <!--            导入-->
          <!--          </el-button>-->
        </template>
        <!-- 用户名列 -->
        <template v-slot:nickname="{ row }">
          <router-link :to="'/system/user/details?id=' + row.userId">
            {{ row.nickname }}
          </router-link>
        </template>
        <!-- 角色列 -->
        <template v-slot:roles="{ row }">
          <el-tag
            v-for="item in row.roles"
            :key="item.roleId"
            size="mini"
            type="primary"
            :disable-transitions="true"
          >
            {{ item.roleName }}
          </el-tag>
        </template>
        <!-- 状态列 -->
        <template v-slot:equipmentStatus="{ row }">
          <el-tag
            size="mini"
            :disable-transitions="true"
            :type="['success', 'danger'][row.equipmentStatus]"
          >
            {{ ['使用中', '闲置中'][row.equipmentStatus] }}
          </el-tag>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除该代理类型吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <user-edit ref="edit" :visible.sync="showEdit" :data="current" @done="reload" />
    <!-- 导入弹窗 -->
    <user-import :visible.sync="showImport" @done="reload" />
  </div>
</template>

<script>
  // import prize from './components/prize.vue'
  import UserSearch from './components/user-search.vue';
  import UserEdit from './components/user-edit.vue';
  import UserImport from './components/user-import.vue';
  import EquipmentEdit from './components/equipment-edit.vue';
  import {
    pageAgencyType,
    removeUser,
    agentTypeDel,
    updateUserStatus,
    updateUserPassword,
    mealPageList
  } from '@/api/oem';
  import {JSEncrypt} from "jsencrypt";

  export default {
    name: 'SystemUser',
    components: {
      UserSearch,
      UserEdit,
      UserImport,
      // prize
    },
    data() {
      return {
        expands:[],
        getRowKeys (row) {
          return row.id
        },
        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center'
          },
          {
            width: 100,
            type: 'expand',
            columnKey: 'expand',
            align: 'center',
            fixed: 'left',
            label:'套餐列表',
            slot: 'expand'
          },
          {
            columnKey: 'index',
            type: 'index',
            label: '序号',
            width: 60,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {

            prop: 'agencyName',
            label: '代理名称',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'remark',
            label: '备注',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 220,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 是否显示导入弹窗
        showImport: false,
        //是否显示划拨编辑弹窗
        showTransfer: false,
        // 展开行 数据
        tableData:[]
      };
    },
    created() {
      this.mealPageLists()
    },
    methods:{
      // 折叠面板每次只能展开一行
      expandChange(row, expandedRows){
        var that = this
        if (expandedRows.length) {
          that.expands = []
          if (row) {
            that.expands.push(row.id)
            console.log("111111111",row)
            this.mealPageLists(row)
          }
        } else {
          that.expands = []
        }
      },
      mealPageLists(row){
        /* 获取角色数据 */
        let parms ={
          types:1
        }
        if(row){
          parms.agentId=row.id
        }
        else{
          parms={
            types:1
          }
        }
        mealPageList(parms)
          .then((list) => {
            this.tableData = list;
            this.$emit('uploadPrice',this.tableData)
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        if (!where.deleted) where.deleted = 0
        return pageAgencyType({ ...where, ...order, page, limit, types:1 });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where, types:1 });
      },
      /* 打开编辑弹窗 */
      openEdit(row) {
        console.log('row====',row)
        this.current = row;
        this.showEdit = true;
        this.$nextTick(()=>{
          this.$refs['edit'].init(row)
        })
      },
      /* 打开划拨编辑弹窗 */
      openTransfer(row) {
        this.current = row;
        this.showTransfer = true;
      },
      /* 打开导入弹窗 */
      openImport() {
        this.showImport = true;
      },
      /* 删除 */
      remove(row) {
        const loading = this.$loading({ lock: true });
        removeUser({ids:[row.id]})
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            this.$message.error(e.message);
          });
      },
      /* 批量删除 */
      removeBatch() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$confirm('确定要删除该代理类型吗?', '提示', {
          type: 'warning'
        })
          .then(() => {
            const loading = this.$loading({ lock: true });
            agentTypeDel({ids: this.selection.map((d) => d.id)})
              .then((msg) => {
                loading.close();
                this.$message.success(msg);
                this.reload();
              })
              .catch((e) => {
                loading.close();
                this.$message.error(e.message);
              });
          })
          .catch(() => {});
      },
    }
  };
</script>
<style>
</style>
